package com.yunji.order.context.convert;

import com.google.common.collect.ImmutableTable;
import com.yunji.common.utils.Pair;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.order.domain.OrderDO;
import com.yunji.order.domain.RefundDO;
import com.yunji.order.dto.OrderDetailRefundDTO;
import com.yunji.order.dto.RefundDetailParam;
import com.yunji.order.emuns.RefundStatusEnum;
import com.yunji.order.emuns.RefundTypeEnum;
import com.yunji.order.service.RefundService;
import com.yunji.order.vo.RefundVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : peter-zhu
 * @date : 2025/3/28 09:18
 * @description : TODO
 **/
public class RefundConvert {

    public static RefundVO domain2VO(RefundDO domain, OrderDO order, RefundService refundService) {
        RefundVO refundVO = new RefundVO();
        BeanUtils.clone(order, refundVO);
        BeanUtils.clone(domain, refundVO);
        refundVO.setDetailList(refundService.refundDetailList(RefundDetailParam.builder().refundId(refundVO.getRefundId()).build()));
        return refundVO;
    }

    public static void convertVO(RefundVO refundVO) {
        refundVO.setApplyStatusDesc(RefundStatusEnum.fromCode(refundVO.getApplyStatus()).getValue());
        refundVO.setRefundTypeDesc(RefundTypeEnum.fromCode(refundVO.getRefundType()).getText());
    }

    public static void convertVO(List<RefundVO> refundVOs) {
        refundVOs.forEach(RefundConvert::convertVO);
    }

    public static ImmutableTable<String, Long, List<Pair<Integer, Integer>>> buildTableNums(List<OrderDetailRefundDTO> refundStatusList) {
        Map<String, Map<Long, Map<Integer, Integer>>> tempMap = buildNumsMap(refundStatusList);
        ImmutableTable.Builder<String, Long, List<Pair<Integer, Integer>>> builder = ImmutableTable.builder();
        for (Map.Entry<String, Map<Long, Map<Integer, Integer>>> rowEntry : tempMap.entrySet()) {
            String orderCode = rowEntry.getKey();
            for (Map.Entry<Long, Map<Integer, Integer>> colEntry : rowEntry.getValue().entrySet()) {
                Long orderDetailId = colEntry.getKey();
                List<Pair<Integer, Integer>> pairList = new ArrayList<>();
                for (Map.Entry<Integer, Integer> valueEntry : colEntry.getValue().entrySet()) {
                    Integer applyStatus = valueEntry.getKey();
                    Integer refundNums = valueEntry.getValue();
                    pairList.add(new Pair<>(applyStatus, refundNums));  // 将每个 applyStatus 和 refundNums 作为 Pair 加入 List
                }
                // 将 orderCode, orderDetailId 和 List<Pair> 放入 ImmutableTable
                builder.put(orderCode, orderDetailId, pairList);
            }
        }
        return builder.build();
    }

    private static Map<String, Map<Long, Map<Integer, Integer>>> buildNumsMap(List<OrderDetailRefundDTO> refundStatusList) {
        Map<String, Map<Long, Map<Integer, Integer>>> tempMap = new HashMap<>();
        for (OrderDetailRefundDTO refundNum : refundStatusList) {
            Map<Long, Map<Integer, Integer>> colunmMap = tempMap.computeIfAbsent(refundNum.getOrderCode(), k -> new HashMap<>());
            Map<Integer, Integer> existingValue = colunmMap.computeIfAbsent(refundNum.getOrderDetailId(), k -> new HashMap<>());
            existingValue.put(refundNum.getApplyStatus(),
                    existingValue.getOrDefault(refundNum.getApplyStatus(), 0) + refundNum.getRefundNums());
        }
        return tempMap;
    }
}
